﻿using System;
using System.Net;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using Silvery.Loader;
using Silvery.Logs;
using System.Windows;


namespace Silvery.Role.Simple
{
    /// <summary>
    /// 这是一个基本的简要角色示例类
    /// 以此作为基础扩展其他的简要角色类
    /// 注意：此示例，将严格按照本项目提供的资源格式，如修改请参看其他的Simple命名空间
    /// </summary>
    public abstract class SimpleRole : Role,ISimpleRoleInfo
    {        
        
        protected NamePanel RoleNamePanel { get; private set; }
        /// <summary>
        /// 角色名字
        /// </summary>
        public string RoleName { get { return RoleNamePanel.RoleName.Text; } set { RoleNamePanel.RoleName.Text = RoleNamePanel._RoleNamesandow.Text = value; } }
        public SimpleRole(Uri bodyUri)
        {
            //添加名字板
            RoleNamePanel = new Simple.NamePanel("Unknown");
            this.Parts.Add("RoleName", RoleNamePanel);

            //使用传入的bodyuri目标下载角色精灵文件
            SetBodyDownLoadUri(bodyUri);
            SimpleRoleShadow = new SimpleShadow();
        }

        protected virtual void SetBodyDownLoadUri(Uri bodyUri)
        {
            if (bodyUri != null)
            {
                spiritloader = new SpiritLoader(bodyUri);
                spiritloader = LoaderManager.Current.Load(spiritloader);
                spiritloader.DownloadCompleted += new OpenReadCompletedEventHandler(spiritloader_DownloadCompleted);
            }
        }
        internal ILoader spiritloader;
        public virtual void BeginDownload()
        {
            if(spiritloader != null)
                spiritloader.BeginDownLoad();
            spiritloader = null;
        }
        /// <summary>
        /// 身体的组件
        /// </summary>
        protected NSpirit BodyPart
        {
            get
            {
                if (Parts.ContainsKey("Body"))
                    return base.Parts["Body"] as NSpirit;
                else 
                    return null;
            }
            set
            {
                if (value == BodyPart)
                    return;
                if (Parts.ContainsKey("Body"))
                {
                    var b = Parts["Body"];
                    if (b is IDisposable)
                        (b as IDisposable).Dispose();
                    Parts.Remove("Body");
                }
                Parts.Add("Body", value);
            }
        }
        /// <summary>
        /// 角色影子设置，可以做另外的影子制作
        /// </summary>
        public BaseObject SimpleRoleShadow
        {
            set
            {                
                Parts.Remove("shadow");
                if (value == null)
                    return;
                this.Parts.Add("shadow", value);
                Canvas.SetZIndex(value, -9999);
            }
        }
        bool mouseEnterEffectOpened = false;
        /// <summary>
        /// 是否开启了选择效果
        /// </summary>
        public bool MouseEnterEffectOpened
        {
            get { return mouseEnterEffectOpened; }
            set
            {
                if (mouseEnterEffectOpened == value) return;
                mouseEnterEffectOpened = value;
                if (mouseEnterEffectOpened)
                {//鼠标移入和移出时候的效果
                    base.MouseEnter += new System.Windows.Input.MouseEventHandler(SimpleRole_MouseEnter);
                    base.MouseLeave += new System.Windows.Input.MouseEventHandler(SimpleRole_MouseLeave);
                }
                else
                {
                    base.MouseEnter -= SimpleRole_MouseEnter;
                    base.MouseLeave -= SimpleRole_MouseLeave;
                    this.Effect = null;
                }
            }
        }
        static void SimpleRole_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            (sender as FrameworkElement).Effect = null;
        }

        static void SimpleRole_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            (sender as FrameworkElement).Effect = RoleMouseEnterEffect.MouseEnterEffect;
        }
        
        void spiritloader_DownloadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            (sender as ILoader).DownloadCompleted -= spiritloader_DownloadCompleted;
            OnBodyPackLoaded(this,(sender as SpiritLoader).SpiritData);
        }
        /// <summary>
        /// 身体下载完成的虚函数
        /// </summary>
        protected virtual void OnBodyPackLoaded()
        {
            BodyPart.IsMirror = IsMirror;
        }
        protected static void OnBodyPackLoaded(SimpleRole simplerole, ISpiritData SpiritData)
        {
            simplerole.BodyPart = new NSpirit();
            simplerole.BodyPart.SpiritData = SpiritData;
            simplerole.OnBodyPackLoaded();
        }
        /// <summary>
        /// 销毁
        /// </summary>
        public override void Dispose()
        {
            MouseEnterEffectOpened = false;
            base.Dispose();
            BodyPart.Dispose();
            BodyPart = null;
        }
        bool _IsMirror;
        public new bool IsMirror
        {
            get { return _IsMirror; }
            set
            {
                if(BodyPart!=null)
                    BodyPart.IsMirror = value;
                _IsMirror = value;
            }
        }

    }
}
